/**
 * @brief 视频融合
 */
class SceneVideo {
  constructor(viewer) {
    this.viewer = viewer;
    this.viewPosition = null;
    this.viewHeading = 0;
    this.viewDistance = 100;
    this.width = 200;
    this.height = 150;
    this.videoDomEle = document.getElementById("videoProjectionVideoEle");
  }
  /**
   * @brief 位置配置
   */
  setConfig() {
    this.viewPosition = Cesium.Cartesian3.fromDegrees(
      113.624802,
      34.743841,
      100
    );
    this.viewHeading = 0;
    this.viewDistance = 100; 
    this.width = 300;
    this.height = 300;
  }
  /**
   * @brief 添加视频投影
   */
  addVideoPolygon() {
    this.setConfig();
    //可视中点
    this.centerPosition = this.GetByDircAndLen(
      this.viewPosition,
      this.viewHeading,
      this.viewDistance
    );

    //斜边长度
    let _hypotenuse = Math.sqrt(
      ((this.width / 2) * this.width) / 2 +
        ((this.height / 2) * this.height) / 2
    );
    //计算角度
    let _angle = Math.asin(this.width / 2 / _hypotenuse) * (180 / Math.PI);
    let _LTPosition = this.GetByDircAndLen(
      this.centerPosition,
      this.viewHeading - _angle,
      _hypotenuse   
    ); //左上角
    let _LBPosition = this.GetByDircAndLen(
      this.centerPosition,
      this.viewHeading + _angle,
      _hypotenuse
    ); //左下角
    let _RTPosition = this.GetByDircAndLen(
      this.centerPosition,
      this.viewHeading - (180 - _angle),
      _hypotenuse
    ); //右上角
    let _RBPosition = this.GetByDircAndLen(
      this.centerPosition,
      this.viewHeading + (180 - _angle),
      _hypotenuse
    ); //右下角
    this.positions = [_LTPosition, _LBPosition, _RBPosition, _RTPosition];
    this.videoPolygonEntity = this.viewer.entities.add({
      id: "videoPolygon",
      polygon: {
        material: Cesium.Color.RED.withAlpha(0.4),
        hierarchy: new Cesium.CallbackProperty(() => {
          return {
            positions: this.positions
          };
        }, false),
        material: this.videoDomEle,
      }
    });
    this.viewer.flyTo(this.videoPolygonEntity);
  }
  //根据旋转和距离计算点
  GetByDircAndLen(position, headingAngle, len) {
    let matrix = Cesium.Transforms.eastNorthUpToFixedFrame(position);
    let mz = Cesium.Matrix3.fromRotationZ(
      Cesium.Math.toRadians(0 - headingAngle || 0)
    );
    let rotationZ = Cesium.Matrix4.fromRotationTranslation(mz);
    Cesium.Matrix4.multiply(matrix, rotationZ, matrix);
    let result = Cesium.Matrix4.multiplyByPoint(
      matrix,
      new Cesium.Cartesian3(0, len, 0),
      new Cesium.Cartesian3()
    );
    return result;
  }
}
export default SceneVideo;
